package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.GoodsClass;
import com.qf.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("class")
public class ClassController {
    @Autowired
    private ClassService service;


    //分页查询
    @RequestMapping("findByClassPage")
    @ResponseBody
    public JsonResult findByClassPage(
           @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
           @RequestParam(value = "limit",required = false,defaultValue = "5") Integer pageSize)
    {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询所有
        List<GoodsClass> allClass = service.findAllClass();
        //封装进PageInfo对象
        PageInfo<GoodsClass> pageInfo = new PageInfo<>(allClass);
        //返回JsonResult对象
        JsonResult ok = JsonResult.ok();
        ok.setCount(pageInfo.getTotal());
        ok.setData(pageInfo.getList());

        return ok;
    }

    //增加
    @RequestMapping("addClass")
    public String addClass(GoodsClass goodsClass){
        System.out.println(goodsClass);
        //调用
        service.addClass(goodsClass);
        return "redirect:/index.html";
    }

    //删除
    @RequestMapping("deleteClassById")
    @ResponseBody
    public String deleteClassById(Integer id) {
        System.out.println(id);
        //调用
        service.deleteClassById(id);
        return "success";
    }

    //修改
    @RequestMapping("updateClass")
    @ResponseBody
    public String updateClass(GoodsClass goodsClass){
        System.out.println(goodsClass);
        //调用
        service.updateClass(goodsClass);
        return "success";
    }
}
